<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\VideoController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\AdminController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

// 用户认证相关接口
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// 测试接口 - 基础信息
Route::get('/test', function () {
    return response()->json([
        'message' => 'API 测试成功！',
        'status' => 'success',
        'timestamp' => now(),
        'version' => '1.0.0'
    ]);
});

// 健康检查接口
Route::get('/health', function () {
    return response()->json([
        'status' => 'healthy',
        'message' => '服务运行正常',
        'timestamp' => now(),
        'server_time' => date('Y-m-d H:i:s'),
        'php_version' => PHP_VERSION
    ]);
});

// 获取服务器信息
Route::get('/info', function () {
    return response()->json([
        'server' => [
            'php_version' => PHP_VERSION,
            'laravel_version' => app()->version(),
            'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
            'memory_usage' => memory_get_usage(true),
            'memory_peak' => memory_get_peak_usage(true)
        ],
        'timestamp' => now()
    ]);
});

// 测试 POST 请求
Route::post('/test-post', function (Request $request) {
    return response()->json([
        'message' => 'POST 请求测试成功HAHAHAHAHAHA',
        'received_data' => $request->all(),
        'method' => $request->method(),
        'timestamp' => now()
    ]);
});

// 测试 PUT 请求
Route::put('/test-put/{id}', function (Request $request, $id) {
    return response()->json([
        'message' => 'PUT 请求测试成功',
        'id' => $id,
        'received_data' => $request->all(),
        'method' => $request->method(),
        'timestamp' => now()
    ]);
});

// 测试 DELETE 请求
Route::delete('/test-delete/{id}', function ($id) {
    return response()->json([
        'message' => 'DELETE 请求测试成功',
        'deleted_id' => $id,
        'timestamp' => now()
    ]);
});

// 模拟用户数据接口
Route::get('/users', function () {
    $users = [
        [
            'id' => 1,
            'name' => '张三',
            'email' => 'zhangsan@example.com',
            'created_at' => '2024-01-01 10:00:00'
        ],
        [
            'id' => 2,
            'name' => '李四',
            'email' => 'lisi@example.com',
            'created_at' => '2024-01-02 11:00:00'
        ],
        [
            'id' => 3,
            'name' => '王五',
            'email' => 'wangwu@example.com',
            'created_at' => '2024-01-03 12:00:00'
        ]
    ];
    
    return response()->json([
        'data' => $users,
        'total' => count($users),
        'message' => '用户列表获取成功'
    ]);
});

// 模拟产品数据接口
Route::get('/products', function () {
    $products = [
        [
            'id' => 1,
            'name' => 'iPhone 15',
            'price' => 7999.00,
            'category' => '手机',
            'stock' => 100
        ],
        [
            'id' => 2,
            'name' => 'MacBook Pro',
            'price' => 12999.00,
            'category' => '电脑',
            'stock' => 50
        ],
        [
            'id' => 3,
            'name' => 'AirPods Pro',
            'price' => 1999.00,
            'category' => '耳机',
            'stock' => 200
        ]
    ];
    
    return response()->json([
        'data' => $products,
        'total' => count($products),
        'message' => '产品列表获取成功'
    ]);
});

// 分页测试接口
Route::get('/posts', function (Request $request) {
    $page = $request->get('page', 1);
    $perPage = $request->get('per_page', 10);
    
    // 模拟数据
    $posts = [];
    for ($i = 1; $i <= 25; $i++) {
        $posts[] = [
            'id' => $i,
            'title' => "测试文章 {$i}",
            'content' => "这是第 {$i} 篇文章的内容...",
            'author' => "作者 {$i}",
            'created_at' => now()->subDays($i)->format('Y-m-d H:i:s')
        ];
    }
    
    $total = count($posts);
    $offset = ($page - 1) * $perPage;
    $paginatedPosts = array_slice($posts, $offset, $perPage);
    
    return response()->json([
        'data' => $paginatedPosts,
        'pagination' => [
            'current_page' => (int)$page,
            'per_page' => (int)$perPage,
            'total' => $total,
            'last_page' => ceil($total / $perPage),
            'from' => $offset + 1,
            'to' => min($offset + $perPage, $total)
        ],
        'message' => '文章列表获取成功'
    ]);
});

// 错误测试接口
Route::get('/error-test', function () {
    return response()->json([
        'error' => '这是一个测试错误',
        'code' => 400,
        'message' => '请求参数错误'
    ], 400);
});

// 延迟测试接口
Route::get('/delay-test', function () {
    sleep(2); // 模拟延迟
    return response()->json([
        'message' => '延迟测试完成',
        'delay' => '2秒',
        'timestamp' => now()
    ]);
});

// 认证相关接口
Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);
Route::post('/admin/login', [AuthController::class, 'adminLogin']);

// 管理员相关接口 - 需要管理员认证
Route::prefix('admin')->middleware('admin')->group(function () {
    Route::get('/stats', [AdminController::class, 'getStats']);
    Route::get('/users', [AdminController::class, 'getUsers']);
    Route::post('/users', [AdminController::class, 'addUser']);
    Route::put('/users/{id}', [AdminController::class, 'updateUser']);
    Route::put('/users/{id}/status', [AdminController::class, 'toggleUserStatus']);
    Route::delete('/users/{id}', [AdminController::class, 'deleteUser']);
    
    // 视频管理
    Route::get('/videos', [AdminController::class, 'getVideos']);
    Route::post('/videos', [AdminController::class, 'addVideo']);
    Route::put('/videos/{id}', [AdminController::class, 'updateVideo']);
    Route::put('/videos/{id}/featured', [AdminController::class, 'toggleVideoFeatured']);
    Route::delete('/videos/{id}', [AdminController::class, 'deleteVideo']);
    
    // 分类管理
    Route::get('/categories', [AdminController::class, 'getCategories']);
    Route::post('/categories', [AdminController::class, 'addCategory']);
    Route::put('/categories/{id}', [AdminController::class, 'updateCategory']);
    Route::put('/categories/{id}/status', [AdminController::class, 'toggleCategoryStatus']);
    Route::delete('/categories/{id}', [AdminController::class, 'deleteCategory']);
    
    // 评论管理
    Route::get('/comments', [AdminController::class, 'getComments']);
    Route::post('/comments', [AdminController::class, 'addComment']);
    Route::put('/comments/{id}', [AdminController::class, 'updateComment']);
    Route::put('/comments/{id}/status', [AdminController::class, 'toggleCommentStatus']);
    Route::post('/comments/batch-approve', [AdminController::class, 'batchApproveComments']);
    Route::post('/comments/batch-reject', [AdminController::class, 'batchRejectComments']);
    Route::delete('/comments/{id}', [AdminController::class, 'deleteComment']);
    
    // 轮播图管理
    Route::get('/banners', [AdminController::class, 'getBanners']);
    Route::post('/banners', [AdminController::class, 'addBanner']);
    Route::put('/banners/{id}', [AdminController::class, 'updateBanner']);
    Route::put('/banners/{id}/status', [AdminController::class, 'toggleBannerStatus']);
    Route::delete('/banners/{id}', [AdminController::class, 'deleteBanner']);
    
            // 批量操作
            Route::post('/batch', [AdminController::class, 'batchAction']);
            
            // 系统设置
            Route::get('/settings', [AdminController::class, 'getSettings']);
            Route::put('/settings', [AdminController::class, 'updateSettings']);
            
            // 系统日志
            Route::get('/logs', [AdminController::class, 'getLogs']);
            Route::delete('/logs', [AdminController::class, 'clearLogs']);
            
            // 系统健康状态
            Route::get('/health', [AdminController::class, 'getSystemHealth']);
        });

// 视频相关接口
Route::prefix('videos')->group(function () {
    Route::get('/', [VideoController::class, 'index']); // 获取视频列表
    Route::get('/banners', [VideoController::class, 'banners']); // 获取轮播图
    Route::get('/recommended', [VideoController::class, 'recommended']); // 获取推荐视频
    Route::get('/{id}', [VideoController::class, 'show']); // 获取视频详情
    Route::post('/{id}/view', [VideoController::class, 'incrementViews']); // 增加播放次数
    Route::post('/{id}/like', [VideoController::class, 'toggleLike']); // 点赞视频
    Route::get('/{id}/comments', [VideoController::class, 'getComments']); // 获取视频评论
    Route::post('/{id}/comments', [VideoController::class, 'addComment']); // 添加评论
});

// 评论相关API
Route::prefix('comments')->group(function () {
    Route::post('/{id}/like', [VideoController::class, 'likeComment']); // 点赞评论
});
